<template>
    <div class="table">

        <div class="container" style="padding: 0;border: 0;">
<!--            基础-->
<!--            {{isEditInfo}}{{parentData}}-->
            <div v-if="tabIndexActive == 'first'">
                <el-form label-width="120px" style="">
                    <el-form-item label="设备类型名称" class="limit-width">
                        <el-input v-model="baseForm.typeName" style="width:46.5%"></el-input>
                    </el-form-item>
<!--                    <el-form-item label="网关协议" class="limit-width">-->
<!--                        <el-select v-model="baseForm.typeName" placeholder="请选择网关协议">-->
<!--                            <el-option v-for="item in dataColList" :label="item.deviceName" :value="item.deviceId"></el-option>-->
<!--                        </el-select>-->
<!--                    </el-form-item>-->
                    <el-form-item label="读取方式" class="limit-width">
                        <el-select v-model="baseForm.readDataMethod" placeholder="请选择读取方式">
                            <el-option v-for="item in readType" :label="item.readTypeName" :value="item.readTypeId" :key="item.readTypeId">
                            </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="设备描述" class="limit-width">
                        <el-input v-model="baseForm.remark" style="width:46.5%"></el-input>
                    </el-form-item>
                    <!--<el-form-item label="型号图片" class="limit-width">-->
                        <!--<el-select v-model="baseForm.imageUrl" placeholder="请选择活动区域">-->
                            <!--<el-option v-for="item in imgList" label="" :value="item" :key="item">-->
                                <!--<img :src="require('../../../assets/img/DeviceImages/'+item)" class="img-list" alt=""><span class="img-list-span">{{item}}</span>-->
                            <!--</el-option>-->
                        <!--</el-select>-->
<!--&lt;!&ndash;                        <img :src="require('../../../assets/img/DeviceImages/'+ baseForm.imageUrl)" alt="" style="position: absolute;">&ndash;&gt;-->
                    <!--</el-form-item>-->
                    <!--<el-form-item label="离线图片" class="limit-width">-->
                        <!--<el-select v-model="baseForm.imageOfflineUrl" placeholder="请选择活动区域">-->
                            <!--<el-option v-for="item in imgList" label="" :value="item" :key="item">-->
                                <!--<img :src="require('../../../assets/img/DeviceImages/'+item)" class="img-list" alt=""><span class="img-list-span">{{item}}</span>-->
                            <!--</el-option>-->
                        <!--</el-select>-->
                    <!--</el-form-item>-->
                    <!--<el-form-item label="告警图片" class="limit-width">-->
                        <!--<el-select v-model="baseForm.imageAlarmUrl" placeholder="请选择活动区域">-->
                            <!--<el-option v-for="item in imgList" label="" :value="item" :key="item">-->
                                <!--<img :src="require('../../../assets/img/DeviceImages/'+item)" class="img-list" alt=""><span class="img-list-span">{{item}}</span>-->
                            <!--</el-option>-->
                        <!--</el-select>-->
                    <!--</el-form-item>-->

                </el-form>


            </div>
<!--            运行-->
            <div v-if="tabIndexActive == 'second'">
                <el-form ref="form" label-width="80px">
                    <el-form-item label="是否启用">
                        <el-checkbox v-model="baseForm.canChangeStatus"></el-checkbox>
                    </el-form-item>
                </el-form>
                <div>
                    <div class="table-class-title">控制参数</div>
                    <div style="margin-bottom: 10px;">
                        <el-button type="primary" @click="addTable">新增</el-button>
                        <el-button type="danger" @click="delTable">删除</el-button>
                    </div>
                </div>
                <el-table :data="controlForm" :header-cell-style="headerCellStyle" style="width: 100%"
                          stripe border class="table" ref="multipleTable" @selection-change="selectChangeTable">
                    <el-table-column
                            type="selection"
                            width="55">
                    </el-table-column>
                    <el-table-column prop="functionName" align="center" label="控制名称">
                        <template slot-scope="scope">
                            <el-input class="no-border-input" v-model="scope.row.functionName" size="mini" placeholder="名称"></el-input>
                        </template>
                    </el-table-column>
                    <el-table-column prop="functionCode" align="center" label="名称编码">
                        <template slot-scope="scope">
                            <el-input class="no-border-input" v-model="scope.row.functionCode" size="mini" placeholder="编码"></el-input>
                        </template>
                    </el-table-column>
                </el-table>
            </div>
<!--            数据-->
            <div v-if="tabIndexActive == 'third'">
                <el-form ref="form" label-width="80px">
                    <el-form-item label="是否启用">
                        <el-checkbox v-model="baseForm.canReadData"></el-checkbox>
                    </el-form-item>
                </el-form>
                <div>
                    <div class="table-class-title">数据参数</div>
                    <div style="margin-bottom: 10px;">
                        <el-button type="primary" @click="addTable">新增</el-button>
                        <el-button type="danger" @click="delTable">删除</el-button>
                    </div>
                </div>
                <el-table :data="deviceData" :header-cell-style="headerCellStyle" style="width: 100%"
                          stripe border class="table" ref="multipleTable" @selection-change="selectChangeTable">
                    <el-table-column
                            type="selection"
                            width="55">
                    </el-table-column>
                    <el-table-column prop="dataName" align="center" label="数据名称">
                        <template slot-scope="scope">
                            <el-input class="no-border-input" v-model="scope.row.dataName" size="mini" placeholder="名称"></el-input>
                        </template>
                    </el-table-column>
                    <el-table-column prop="dataUnit" align="center" label="数据单位">
                        <template slot-scope="scope">
                            <el-input class="no-border-input" v-model="scope.row.dataUnit" size="mini" placeholder="单位"></el-input>
                        </template>
                    </el-table-column>
                    <el-table-column prop="dataSymbol" align="center" label="数据符号">
                        <template slot-scope="scope">
                            <el-input class="no-border-input" v-model="scope.row.dataSymbol" size="mini" placeholder="符号"></el-input>
                        </template>
                    </el-table-column>
                    <!--<el-table-column prop="dataRange" align="center" label="量程">-->
                        <!--<template slot-scope="scope">-->
                            <!--<el-input class="no-border-input" v-model="scope.row.dataRange" size="mini" placeholder="量程"></el-input>-->
                        <!--</template>-->
                    <!--</el-table-column>-->
                    <!--<el-table-column prop="dataRatio" align="center" label="互感比">-->
                        <!--<template slot-scope="scope">-->
                            <!--<el-input class="no-border-input" v-model="scope.row.dataRatio" size="mini" placeholder="互感比"></el-input>-->
                        <!--</template>-->
                    <!--</el-table-column>-->
                </el-table>

            </div>
<!--        告警-->
            <div v-if="tabIndexActive == 'fourth'">
                <el-form ref="form" label-width="80px">
                    <el-form-item label="是否启用">
                        <el-checkbox v-model="checked"></el-checkbox>
                    </el-form-item>
                </el-form>
                <div>
                    <div class="table-class-title">状态配置</div>
                    <div style="margin-bottom: 10px;">
                        <el-button type="primary" @click="addTable">新增</el-button>
                        <el-button type="danger" @click="delTable">删除</el-button>
                    </div>
                </div>
                <el-table :data="statusDevice" :header-cell-style="headerCellStyle" style="width: 100%"
                          stripe border class="table" ref="multipleTable" @selection-change="selectChangeTable">
                    <el-table-column
                            type="selection"
                            width="55">
                    </el-table-column>
                    <el-table-column prop="statusName" align="center" label="状态名称">
                        <template slot-scope="scope">
                            <el-input class="no-border-input" v-model="scope.row.statusName" size="mini" placeholder="名称"></el-input>
                        </template>
                    </el-table-column>
                    <el-table-column prop="statusCode" align="center" label="状态编码">
                        <template slot-scope="scope">
                            <el-input class="no-border-input" v-model="scope.row.statusCode" size="mini" placeholder="编码"></el-input>
                        </template>
                    </el-table-column>
                </el-table>

            </div>
            <!--            modbus-->
            <div v-if="tabIndexActive == 'five'">
                <!--<el-form ref="form" label-width="80px">-->
                    <!--<el-form-item label="是否启用">-->
                        <!--<el-checkbox v-model="baseForm.canReadData"></el-checkbox>-->
                    <!--</el-form-item>-->
                <!--</el-form>-->
                <div>
                    <div class="table-class-title">数据参数</div>
                    <div style="margin-bottom: 10px;">
                        <el-button type="primary" @click="addTable">新增</el-button>
                        <el-button type="danger" @click="delTable">删除</el-button>
                    </div>
                </div>
                <el-table :data="busData" :header-cell-style="headerCellStyle" style="width: 100%"
                          stripe border class="table" ref="multipleTable" @selection-change="selectChangeTable">
                    <el-table-column
                            type="selection"
                            width="55">
                    </el-table-column>
                    <el-table-column prop="dataName" align="center" label="数据名称">
                        <template slot-scope="scope">
                            <el-input class="no-border-input" v-model="scope.row.dataName" size="mini" placeholder="名称"></el-input>
                        </template>
                    </el-table-column>
                    <el-table-column prop="dataUnit" align="center" label="数据单位">
                        <template slot-scope="scope">
                            <el-input class="no-border-input" v-model="scope.row.dataUnit" size="mini" placeholder="单位"></el-input>
                        </template>
                    </el-table-column>
                    <el-table-column prop="dataSymbol" align="center" label="数据符号">
                        <template slot-scope="scope">
                            <el-input class="no-border-input" v-model="scope.row.dataSymbol" size="mini" placeholder="单位"></el-input>
                        </template>
                    </el-table-column>
                    <el-table-column prop="dataRange" align="center" label="量程">
                        <template slot-scope="scope">
                            <el-input class="no-border-input" v-model="scope.row.dataRange" size="mini" placeholder="单位"></el-input>
                        </template>
                    </el-table-column>
                    <el-table-column prop="dataRatio" align="center" label="换算比">
                        <template slot-scope="scope">
                            <el-input class="no-border-input" v-model="scope.row.dataRatio" size="mini" placeholder="单位"></el-input>
                        </template>
                    </el-table-column>
                    <el-table-column prop="regAddress" align="center" label="寄存器地址">
                        <template slot-scope="scope">
                            <el-input class="no-border-input" v-model="scope.row.regAddress" size="mini" placeholder="地址"></el-input>
                        </template>
                    </el-table-column>
                    <el-table-column prop="regNum" align="center" label="寄存器个数">
                        <template slot-scope="scope">
                            <el-input class="no-border-input" v-model="scope.row.regNum" size="mini" placeholder="个数"></el-input>
                        </template>
                    </el-table-column>
                    <el-table-column prop="regType" align="center" label="寄存器类型">
                        <template slot-scope="scope">
                            <el-select v-model="scope.row.regType"  placeholder="请选择" >
                                <el-option
                                        v-for="item in selectConcentrator"
                                        :key="item.deviceId"
                                        :label="item.deviceName"
                                        :value="item.deviceId">
                                </el-option>
                            </el-select>
                        </template>
                    </el-table-column>
                    <el-table-column prop="parseType" align="center" label="解析方式">
                        <template slot-scope="scope">
                            <el-select v-model="scope.row.parseType"  placeholder="请选择" >
                                <el-option
                                        v-for="item in parseTypeOption"
                                        :key="item.deviceId"
                                        :label="item.deviceName"
                                        :value="parseInt(item.deviceId)">
                                </el-option>
                            </el-select>
                        </template>
                    </el-table-column>
                </el-table>

            </div>


        </div>


        <!-- 删除提示框 -->
        <el-dialog title="提示" :visible.sync="delVisible" width="300px" center>
            <div class="del-dialog-cnt">删除不可恢复，是否确定删除？</div>
            <span slot="footer" class="dialog-footer">
                <el-button @click="delVisible = false">取 消</el-button>
                <el-button type="primary" @click="deleteRow">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>
<script>
    import { delCountManger ,deleteFunctionOfDeviceType,deleteDataListByDeviceTypeId,deleteStateListByDeviceTypeId,getDataCollectorListAll} from '../../../api/index';
    import {clearObjValue} from '../../common/com';
    import {imgList} from '../../common/img';
    export default {
        name: "Equiment",
        props:['parentData','tabIndexActive','isEditInfo','isShowDialog'],
        data() {
            return {
                cur_page: 1,
                page_size:10,
                page_total:1,
                multipleSelection: [],
                diologTitle:'新增账户',
                delVisible: false,
                checked:false,
                baseForm: {
                    typeName: '',
                    remarks:'',
                    readDataMethod:'',
                    canChangeStatus:false,
                    canPowerLimit:false,
                    canReadData:false,
                    canReadDirection:false,
                    canReadStatus:false,
                    canWarning:false,
                    imageUrl:'',
                    imageOfflineUrl:'',
                    imageAlarmUrl:''
                },
                controlForm:[
                //     {
                //     functionName:'',
                //     functionCode:'',
                // }
                ],
                statusDevice:[
                //     {
                //     statusName:'',
                //     statusCode:''
                // }
                ],
                deviceData:[
                //     {
                //     dataName:'',
                //     dataUnit:'',
                //     dataSymbol:'',
                //     dataRange:'',
                //     dataRatio:'',
                // }
                ],
                busData:[],
                // isEditInfo:false,
                selectList:[],
                imgList:imgList(),
                dataColList:[],
                readType:[{
                    readTypeName:'通过单项数据协议读取',readTypeId:'0'
                },{
                    readTypeName:'通过多项数据协议读取',readTypeId:'1'
                }],
                selectConcentrator: [
                    {deviceName:'保存寄存器(0x04)',deviceId:'4'},
                    {deviceName:'输入寄存器(0x03)',deviceId:'3'}
                ],//获取网关下拉1
                dataConcentrator:[
                    {deviceName:'输入框',deviceId:'1'},
                    {deviceName:'开关',deviceId:'2'}
                ],
                parseTypeOption:[
                    {deviceName:'二进制，先高后低',deviceId:'0'},
                    {deviceName:'二进制，先低后高',deviceId:'1'},
                    {deviceName:'BCD码，先高后低',deviceId:'2'},
                    {deviceName:'BCD码，先低后高',deviceId:'3'},
                    {deviceName:'ASCII，先高后低',deviceId:'4'},
                    {deviceName:'ASCII，先低后高',deviceId:'5'},
                    {deviceName:'2字节无符号，先高后低',deviceId:'6'},
                    {deviceName:'2字节无符号，先低后高',deviceId:'7'},
                    {deviceName:'2字节有符号，先高后低',deviceId:'8'},
                    {deviceName:'2字节有符号，先低后高',deviceId:'9'},
                    {deviceName:'2字节无符号，取高字节',deviceId:'10'},
                    {deviceName:'2字节无符号，取低字节',deviceId:'11'},
                    {deviceName:'2字节有符号，取高字节',deviceId:'12'},
                    {deviceName:'2字节有符号，取低字节',deviceId:'13'},
                    {deviceName:'4字节无符号，顺序4321',deviceId:'14'},
                    {deviceName:'4字节无符号，顺序3412',deviceId:'15'},
                    {deviceName:'4字节无符号，顺序2143',deviceId:'16'},
                    {deviceName:'4字节无符号，顺序1234',deviceId:'17'},
                    {deviceName:'4字节有符号，顺序4321',deviceId:'18'},
                    {deviceName:'4字节有符号，顺序3412',deviceId:'19'},
                    {deviceName:'4字节有符号，顺序2143',deviceId:'20'},
                    {deviceName:'4字节有符号，顺序1234',deviceId:'21'},
                    {deviceName:'4字节短浮点，顺序4321',deviceId:'22'},
                    {deviceName:'4字节短浮点，顺序3412',deviceId:'23'},
                    {deviceName:'4字节短浮点，顺序2143',deviceId:'24'},
                    {deviceName:'4字节短浮点，顺序1234',deviceId:'25'},
                    {deviceName:'4字节长浮点，顺序4321',deviceId:'26'},
                    {deviceName:'4字节长浮点，顺序3412',deviceId:'27'},
                    {deviceName:'4字节长浮点，顺序2143',deviceId:'28'},
                    {deviceName:'4字节长浮点，顺序1234',deviceId:'29'},
                    {deviceName:'8字节长浮点，顺序87654321',deviceId:'30'},
                    {deviceName:'8字节长浮点，顺序65872143',deviceId:'31'},
                    {deviceName:'8字节长浮点，顺序12345678',deviceId:'32'},
                    {deviceName:'8字节长浮点，顺序34127856',deviceId:'33'},
                    {deviceName:'8字节整形12345678',deviceId:'34'},
                    {deviceName:'8字节整形87654321',deviceId:'35'},
                    {deviceName:'位',deviceId:'36'},
                ]
            }
        },
        created() {
            // this.getDataCollectorListAll();
        },
        computed: {

        },
        watch:{
            isShowDialog:function(val){
                if(val && this.isEditInfo){
                    this.baseForm = this.parentData.deviceType;
                    this.baseForm.readDataMethod = this.parentData.deviceType.readDataMethod?'1':'0';
                    this.controlForm = this.parentData.deviceTypeFuncList;
                    this.statusDevice = this.parentData.deviceStatusList;
                    this.deviceData = this.parentData.deviceTypeDataList;
                    this.busData = this.parentData.modbusList;
                }else{
                    this.baseForm = clearObjValue(this.baseForm)
                    this.controlForm = [];
                    this.statusDevice = [];
                    this.deviceData = [];
                    this.busData=[];
                }
            },

        },
        methods: {
            //获取网关
            getDataCollectorListAll(){
                getDataCollectorListAll({}).then((res)=>{
                    console.log(res)
                    this.dataColList = res.datas;
                })
            },
            //表头样式
            headerCellStyle(row,rowIndex){
                return {height:"20px",padding:'2px'}
            },
            delTable(){

                if(this.tabIndexActive == 'second'){
                    this.deleDeviceItem(this.tabIndexActive,this.controlForm)
                }
                if(this.tabIndexActive == 'third'){
                    this.deleDeviceItem(this.tabIndexActive,this.deviceData)
                }
                if(this.tabIndexActive == 'fourth'){
                    this.deleDeviceItem(this.tabIndexActive,this.statusDevice)
                }
                if(this.tabIndexActive == 'five'){
                    this.deleDeviceItem(this.tabIndexActive,this.busData)
                }
            },
            addTable(){
                if(this.tabIndexActive == 'second'){
                    this.controlForm.push({
                        functionName:'',
                        functionCode:'',
                    })
                }
                if(this.tabIndexActive == 'third'){
                    this.deviceData.push({
                        dataName:'',
                        dataUnit:'',
                        dataSymbol:'',
                        dataRange:'',
                        dataRatio:''
                    })
                }
                if(this.tabIndexActive == 'fourth'){
                    this.statusDevice.push({
                        statusName:'',
                        statusCode:'',
                    })
                }
                if(this.tabIndexActive == 'five'){
                    this.busData.push({
                        dataName: '',//设备名称
                        dataUnit:'',
                        dataSymbol:'',
                        dataRange:'',
                        dataRatio:'1',
                        regType:'',
                        regNum:'',
                        regAddress:'',
                        parseType:''
                    })
                }
            },
            selectChangeTable(val){
                this.selectList = [];
                this.selectList = val
                console.log(this.selectList)
            },

            // 确定删除
            deleteRow(){
                var delCountId = new Array();
                for(var i=0;i<this.multipleSelection.length;i++){
                    delCountId.push(this.multipleSelection[i].id)
                }
                console.log(delCountId);
                delCountManger({
                    ids:delCountId
                }).then((res)=>{
                    console.log(res)
                    if(res.success){
                        this.$message.success('删除成功');
                        this.delVisible = false;
                    }else{
                        this.$message.error(res.message)
                    }
                })

            },

            //设备类型控制功能删除
            deleteFunctionOfDeviceType(id){
                deleteFunctionOfDeviceType({
                    id:id
                }).then((res)=>{
                    if(res.success){
                        this.$message.success(res.message);
                        for(var i=0;i<this.controlForm.length;i++){
                            if(this.controlForm[i].functionId == this.selectList[0].functionId){
                                this.controlForm.splice(i,1)
                            }
                        }

                    }else{
                        this.$message.error(res.message)
                    }
                })
            },
            //设备类型数据配置删除
            deleteDataListByDeviceTypeId(id){
                deleteDataListByDeviceTypeId({
                    id:id
                }).then((res)=>{
                    if(res.success){
                        this.$message.success(res.message);
                        for(var i=0;i<this.controlForm.length;i++){
                            if(this.deviceData[i].dataId == this.selectList[0].dataId){
                                this.deviceData.splice(i,1)
                            }
                        }
                    }else{
                        this.$message.error(res.message)
                    }
                })
            },
            //设备类型状态参数删除
            deleteStateListByDeviceTypeId(id){
                deleteStateListByDeviceTypeId({
                    id:id
                }).then((res)=>{
                    if(res.success){
                        this.$message.success(res.message);
                        for(var i=0;i<this.controlForm.length;i++){
                            if(this.statusDevice[i].statusId == this.selectList[0].statusId){
                                this.statusDevice.splice(i,1)
                            }
                        }
                    }else{
                        this.$message.error(res.message)
                    }
                })
            },
            deleDeviceItem(num,delData){
                if(this.selectList.length<=0){
                    this.$message.error('请选择要删除的项');
                    return ;
                }
                if(this.selectList.length>1){
                    this.$message.error('一次只能删除一项');
                    return ;
                }
                for(var i=0;i<delData.length;i++){
                    if(this.selectList.length<=0){
                        return;
                    }
                    if(num == 'second'){
                        if(this.selectList[0].functionId){
                            if(delData[i].functionId == this.selectList[0].functionId){
                                this.deleteFunctionOfDeviceType(this.selectList[0].functionId)
                            }
                        }else{
                            if(delData[i].functionCode == this.selectList[0].functionCode && delData[i].functionName == this.selectList[0].functionName){
                                delData.splice(i,1)
                                this.selectList = [];
                            }
                        }
                    }
                    if(num == 'third'){
                        if(this.selectList[0].dataId){
                            if(delData[i].dataId == this.selectList[0].dataId){
                                this.deleteDataListByDeviceTypeId(this.selectList[0].dataId)
                            }
                        }else{
                            if(delData[i].dataName == this.selectList[0].dataName && delData[i].dataUnit == this.selectList[0].dataUnit){
                                delData.splice(i,1)
                                this.selectList = [];
                            }
                        }
                    }
                    if(num == 'fourth'){
                        if(this.selectList[0].statusId){
                            if(delData[i].statusId == this.selectList[0].statusId){
                                this.deleteStateListByDeviceTypeId(this.selectList[0].statusId)
                            }
                        }else{
                            if(delData[i].statusName == this.selectList[0].statusName && delData[i].statusCode == this.selectList[0].statusCode){
                                delData.splice(i,1)
                                this.selectList = [];
                            }
                        }
                    }
                    if(num == 'five'){
                        if(this.selectList[0].dataId){
                            if(delData[i].dataId == this.selectList[0].dataId){
                                this.deleteDataListByDeviceTypeId(this.selectList[0].dataId)
                            }
                        }else{
                            if(delData[i].dataName == this.selectList[0].dataName && delData[i].dataUnit == this.selectList[0].dataUnit){
                                delData.splice(i,1)
                                this.selectList = [];
                            }
                        }
                    }
                }
            }
        }
    }
</script>

<style scoped>
    .handle-box {
        margin-bottom: 20px;
    }

    .handle-select {
        width: 120px;
    }

    .handle-input {
        width: 300px;
        display: inline-block;
    }
    .del-dialog-cnt{
        font-size: 16px;
        text-align: center
    }
    .table{
        width: 100%;
        font-size: 14px;
    }
    .red{
        color: #ff0000;
    }
    .mr10{
        margin-right: 10px;
    }
    .limit-width{
        /*width: 30%;*/
        /*float: left;*/
        margin:0 10% 10px;

    }
    .el-form-item::after, .el-form-item::before {
        clear: both;
    }
    /deep/ .no-border-input input.el-input__inner{
            border: 0;

    }
    .table-class-title{
        background: #eee;
        padding: 5px 20px;
        margin-bottom: 10px;
    }
    .img-list{
        vertical-align: middle;
    }
    .img-list-span{
        margin-left: 10px;
        position: absolute;
        top: 0;
    }
</style>